package com.zzb.framework.modules.event.controller;

import com.zzb.framework.common.BasePage;
import com.zzb.framework.modules.event.entity.SysEventNotify;

import com.zzb.framework.modules.event.form.SysEventNotifySearchForm;
import com.zzb.framework.modules.event.service.SysEventNotifyService;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.FrameworkException;
import com.zzb.framework.modules.log.annotation.SysLog;
import com.zzb.framework.modules.role.controller.AbstractController;
import com.zzb.framework.modules.role.entity.SysUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 事件通知方法controller
 */
@RestController
@RequestMapping("/sys/eventNotify")
public class SysEventNotifyController extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(SysEventNotifyController.class);
    @Autowired
    private SysEventNotifyService sysEventNotifyService;

    /**
     * 事件通知方法列表json
     */
    @GetMapping("list")
    @RequiresPermissions("sys:event:list")
    public BasePage<SysEventNotify> list(SysEventNotifySearchForm sysEventNotifySearchForm) {
        return sysEventNotifyService.queryPage(sysEventNotifySearchForm);
    }


    @GetMapping("info/{id}")
    @RequiresPermissions("sys:event:info")
    public BaseResponse<SysEventNotify> info(@PathVariable String id) {
        SysEventNotify sysEventNotify = sysEventNotifyService.get(id);
        return BaseResponse.content(sysEventNotify);
    }

    /**
     * 事件通知方法保存
     */
    @PostMapping("/save")
    @SysLog("保存事件")
    @RequiresPermissions("sys:event:save")
    public BaseResponse<SysEventNotify> save(@RequestBody SysEventNotify sysEventNotify) {
        try {
            SysUserEntity user = getUser();
            return sysEventNotifyService.saveSysEventNotify(sysEventNotify, user);
        } catch (FrameworkException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL, e.getMessage());
        }
    }

    /**
     * 事件通知方法删除
     */
    @PostMapping("/delete")
    @SysLog("删除")
    @RequiresPermissions("sys:event:delete")
    public BaseResponse delete(String id, HttpServletRequest request) {
        try {
            return sysEventNotifyService.deleteSysEventNotify(id, getUser());
        } catch (FrameworkException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL, e.getMessage());
        }
    }

    /**
     * 事件通知方法开启
     */
    @RequestMapping("/open")
    public BaseResponse open(String id, HttpServletRequest request) {
        try {
            return sysEventNotifyService.recoveryById(id, getUser());
        } catch (FrameworkException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL, e.getMessage());
        }
    }

    /**
     * 事件通知方法屏蔽
     */
    @RequestMapping("/deny")

    public BaseResponse deny(String id, HttpServletRequest request) {
        try {
            return sysEventNotifyService.trashById(id, getUser());
        } catch (FrameworkException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL, e.getMessage());
        }
    }

}
